package com.baosight.sczx.common;

import java.util.ArrayList;
import java.util.List;

import com.baosight.iplat4j.core.FrameworkInfo;
import com.baosight.iplat4j.core.threadlocal.UserSession;
import com.baosight.iplat4j.es.domain.ESPost;
import com.baosight.iplat4j.es.domain.ESUser;
import com.baosight.iplat4j.security.bridge.SecurityBridge;
import com.baosight.iplat4j.security.bridge.SecurityBridgeFactory;

public class UserESDAO {

	/**
	 * 获取当前登录用户信息
	 * @return ESUser
	 */
	public static ESUser getLoginUser() {
		String loginName = UserSession.getLoginName();
		SecurityBridge b = SecurityBridgeFactory.getBridge();
		return (ESUser) b.getUser(loginName);
	}
	
	/**
	 * 根据id获取用户信息
	 * @return ESUser
	 */
	public static ESUser getESUserById(String userId) {
		SecurityBridge b = SecurityBridgeFactory.getBridge();
		ESUser user = (ESUser) b.getUser(userId);
		return user;
	}
	/**
	 *  获取当前登录用户所属部门id
	 * @return
	 */
	public static String getLoginOrgId()
	{
		SecurityBridge b = SecurityBridgeFactory.getBridge(); 
		// 获取用户在当前工程所有所属角色列表
		String project = FrameworkInfo.getProjectEname();// 获取当前工程名称
		String loginName = UserSession.getLoginName();
		@SuppressWarnings("unchecked")
		List<ESPost> roles = b.getUserRoles(loginName, project);// 获取当前用户所属的角色列表
		
		for (int i = 0; i < roles.size(); i++) {

			ESPost role = roles.get(i);
			String postTypeLabel = role.getPostTypeLabel();		
			if(!"JJ".equals(postTypeLabel)&&!"SC".equals(postTypeLabel)&&!"DD".equals(postTypeLabel)&&!"CM".equals(postTypeLabel)&&!"CC".equals(postTypeLabel)){
				continue;
			}else{
				return roles.get(i).getOrgLabel();
			}
		}
		return "";
	}
	
	/**
	 * 获取所有人员
	 * @param userName
	 * @return List<ESUser>
	 */
	@SuppressWarnings("unchecked")
	public static List<ESUser> getUserList(String userId) {
		List<ESUser> users = new ArrayList<ESUser>();
		SecurityBridge b = SecurityBridgeFactory.getBridge(); 
		// 获取用户在当前工程所有所属角色列表
		String project = FrameworkInfo.getProjectEname();// 获取当前工程名称 
		List<ESPost> roles = b.getUserRoles(userId, project);// 获取当前用户所属的角色列表
		for (int i = 0; i < roles.size(); i++) {
			ESPost role = roles.get(i);
			String postLable = role.getPostLabel();
			users.addAll(b.getRoleMembers(postLable, "", "", 0, -1)); 
		}
		return users;
	}

}
